package by.msokoloff.javaweb2015.webapp.page.admuser;

import java.util.ArrayList;
import java.util.Arrays;

import javax.inject.Inject;

import org.apache.wicket.bean.validation.PropertyValidator;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.IModel;

import by.msokoloff.javaweb2015.datamodel.AdmUser;
import by.msokoloff.javaweb2015.datamodel.enums.AdmUserRole;
import by.msokoloff.javaweb2015.datamodel.enums.AdmUserStatus;
import by.msokoloff.javaweb2015.services.AdmUserService;
import by.msokoloff.javaweb2015.webapp.page.EntityListPage;

public class AdmUserForm extends Form<AdmUser> {

	@Inject
	private AdmUserService admUserService;

	private AdmUser admUser;

	public AdmUserForm(String id, IModel<AdmUser> model) {
		super(id, model);
		
		add(new Label("id"));
		TextField<String> loginField = new TextField<String>("login");
		loginField.add(new PropertyValidator<String>());
		add(loginField);
		TextField<String> passwordField = new PasswordTextField("password");
		passwordField.add(new PropertyValidator<String>());
		add(passwordField);
		TextField<String> nameField = new TextField<String>("name");
		nameField.add(new PropertyValidator<String>());
		add(nameField);
		TextField<String> emailField = new TextField<String>("email");
		emailField.add(new PropertyValidator<String>());
		add(emailField);
		DropDownChoice<AdmUserStatus> statusChoice = new DropDownChoice<AdmUserStatus>("status", new ArrayList<AdmUserStatus>(Arrays.asList(AdmUserStatus.values())));
		statusChoice.add(new PropertyValidator<AdmUserStatus>());
		add(statusChoice);
		DropDownChoice<AdmUserRole> roleChoice = new DropDownChoice<AdmUserRole>("role", new ArrayList<AdmUserRole>(Arrays.asList(AdmUserRole.values())));
		roleChoice.add(new PropertyValidator<AdmUserRole>());
		add(roleChoice);
		add(new FeedbackPanel("feedbackpanel"));
		
		admUser = model.getObject();
	}

	@Override
	public final void onSubmit() {
		admUserService.createOrUpdate(admUser);
		setResponsePage(new EntityListPage(AdmUser.class));
	}

	@Override
	public final void onError() {
		System.out.println("Error!");
	}

}
